package aur.sss.spots;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class DonwloadSpotsHtml {

	static final String PATH_URL_WEB  = "http://www.xtremstudio.com/spots/";
	static final int NUM_SPOT_BUSCAR  = 5000;
	static final int RESPONSE_CODE_OK = 200;
	static final String SAVE_FOLDER   = "c://temp/spots/";
	static FileWriter logFile =null;
	static BufferedWriter logFilebuffer = null;
	
	
	public DonwloadSpotsHtml() {
			super();
	}
	
	
	 public static void main(String args[]){
		 
		try {
			logFile = new FileWriter(SAVE_FOLDER + "log.txt");
			logFilebuffer = new BufferedWriter(logFile);	
			for ( int i=0; i <NUM_SPOT_BUSCAR; i++ ){
				donwnloadSpot( i+".html");
				System.out.println(i+".html");
			}	
			
		} catch (IOException e) {
			System.out.println("Error creando fichero Log");
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if ( logFilebuffer!=null ){
				try {
					logFilebuffer.close();
				} catch (IOException e) {					
					e.printStackTrace();
				}
			}	
			if ( logFile!=null ){
				try {
					logFile.close();
				} catch (IOException e) {					
					e.printStackTrace();
				}
			}	
			
		}
		 
	 }
	
	
	static void donwnloadSpot( String spot_path){

		try {
			
			String spot_url_path = PATH_URL_WEB + spot_path;
			logFilebuffer.write( spot_url_path + " - " );
			URL myURL = new URL( spot_url_path );
			
			int response_cod =getResponseCode(spot_url_path);
			if ( response_cod == RESPONSE_CODE_OK ){

				URLConnection conn = myURL.openConnection();
				//logFilebuffer.write("Spot connected");
				// open the stream and put it into BufferedReader
				BufferedReader br = new BufferedReader(new InputStreamReader(
						conn.getInputStream()));

				String inputLine;
				// save to this filename			
				File file = new File(SAVE_FOLDER + spot_path );

				if (!file.exists()) {
					file.createNewFile();
				}

				// use FileWriter to write file
				FileWriter fw = new FileWriter(file.getAbsoluteFile());
				BufferedWriter bw = new BufferedWriter(fw);

				while ((inputLine = br.readLine()) != null) {
					bw.write(inputLine);
				}

				bw.close();
				br.close();
				logFilebuffer.write ("Spot saved OK ");

				
			}else{
				logFilebuffer.write( "FALLO Response code= " + response_cod);
			}	
			
			logFilebuffer.write ("\n");
			
						
		} catch (MalformedURLException e) {
			System.out.println("URL mal formada");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println(" IOException");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println(" Exception general");
			e.printStackTrace();
		}

	}
	
	/* Return the response code from URL
	 * @see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes	
	 *  
	 */
	public static int getResponseCode(String urlString) throws MalformedURLException, IOException {
	    URL u = new URL(urlString); 
	    HttpURLConnection huc =  (HttpURLConnection)  u.openConnection(); 
	    huc.setRequestMethod("GET"); 
	    huc.connect(); 
	    return huc.getResponseCode();
	}
	

}
